package net.sxnic.ugr.dept.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import net.sxnic.comm.utils.CommUtils;
import net.sxnic.ugr.UgrConstant;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * Comments: 部门代码序列置换部门简称的tag类 <p>
 * Author：孙宇飞<p> 
 * Create Date：2010-6-9<p>
 *
 *
 */
public class DeptValueTag extends BodyTagSupport {

	private static final long serialVersionUID = -2211210255097241285L;

	private String deptCode;
	
	private String deptSplit;

	public int doEndTag() {
		JspWriter out = pageContext.getOut();
		try {
			if (StringUtils.isBlank(deptCode)) {
				out.print("");
			}if("合计".equals(deptCode)){
				out.print("合计");
			} 			
			else {
				deptCode = CommUtils.splitTrim(deptCode, ",");
				String[] codes = StringUtils.split(deptCode, ",");

				String temp = "";

				for (String code : codes) {
					
					temp = temp + UgrConstant.DEPT_MAP.get(code.trim()) ;
					
					if(StringUtils.isNotBlank(deptSplit))
						temp = temp + deptSplit;
					else
						temp = temp + ",";
				}
				
				if(StringUtils.isNotBlank(deptSplit))
					out.print(StringUtils.removeEnd(temp, deptSplit));
				else
					out.print(StringUtils.removeEnd(temp, ","));
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return EVAL_PAGE;
	}

	public String getDeptCode() {
		return deptCode;
	}

	public void setDeptCode(String deptCode) {
		this.deptCode = deptCode;
	}

	public String getDeptSplit() {
		return deptSplit;
	}

	public void setDeptSplit(String deptSplit) {
		this.deptSplit = deptSplit;
	}

}
